/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */
package acid.spring.jdbc.repositories;

import acid.spring.db.Experiment;
import acid.spring.db.ExperimentAnalysis;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 *
 * @author morgan
 */
@Repository
public class JdbcExperimentAnalysisRepository implements ExperimentAnalysisRepository, InitializingBean
{

    @Value("${jdbc.sql.analyses.queryByExperiment}")
    private String experimentAnalysisByExperimentQuery;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<ExperimentAnalysis> getExperimentAnalysisByExperiment(Experiment experiment)
    {
        return jdbcTemplate.query(experimentAnalysisByExperimentQuery, 
                experimentAnalysisRowMapper, experiment.getExperiment_id());
    }

    @Override
    public void afterPropertiesSet() throws Exception
    {
        Assert.notNull(this.jdbcTemplate, "the jdbcTemplate can't be null!");
        Assert.notNull(this.experimentAnalysisByExperimentQuery, "the experimentAnalysisByExperimentQuery can't be null");
    }
    private RowMapper<ExperimentAnalysis> experimentAnalysisRowMapper = new RowMapper<ExperimentAnalysis>()
    {

        @Override
        public ExperimentAnalysis mapRow(ResultSet resultSet, int i) throws SQLException
        {
            String blank = "This space intentionally left blank";
            int id = resultSet.getInt("analysis_id");
            Experiment experiment = new Experiment(resultSet.getInt("experiment_id"), blank, blank, blank);
            String ctlPop = resultSet.getString("ctl_population");
            String expPop = resultSet.getString("exp_population");
            String method = resultSet.getString("analysis_method");
            return new ExperimentAnalysis(id, experiment, ctlPop, expPop,
                    method);
        }
    };
}
